<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleleaf.org">
<head th:insert="base">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="/back/welcome">首页</a>
            <a href="#">用户管理</a>
            <a>
              <cite>用户列表</cite></a>
          </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <div class="layui-form layui-col-space5">
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" id="userAccount"  placeholder="请输入系统账号" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" id="nickName"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" id="userName"  placeholder="请输入姓名" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" id="userEmail"  placeholder="请输入用户邮箱" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" id="userTel"  placeholder="请输入用户电话" autocomplete="off" class="layui-input">
                        </div>
                        <br/>
                        <div class="layui-inline layui-show-xs-block">
                            <input type="text" id="areaCode"  placeholder="请输入区号" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-input-inline layui-show-xs-block">
                            <select id="userSex">
                            </select>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" readonly  autocomplete="off" placeholder="创建开始日期" id="createTimeStart">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <input class="layui-input" readonly  autocomplete="off" placeholder="创建截止日期" id="createTimeEnd">
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" id="formSearch"><i class="layui-icon">&#xe615;</i></button>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-hide" id="tableId" lay-filter="tableId"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
        <button class="layui-btn" id="import"><i class="layui-icon layui-icon-add-1"></i>导入用户数据</button>
    </div>
</script>
<script type="text/html" id="operations">
    <a class="layui-btn layui-btn-xs" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="resetPassword" title="重置密码"><i class="layui-icon layui-icon-key"></i>重置密码</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" title="删除"><i class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script th:inline="none">
    layui.use(['table','form','enums','util','tool','laydate','upload'], function(){
        var table = layui.table;
        var $ = layui.jquery;
        var form = layui.form;
        var enums = layui.enums;
        var util = layui.util;
        var tool = layui.tool;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var baseUrl = "/back/admin";
        init_form();
        table.render({
            elem: '#tableId'
            ,id:'tableReload'
            ,url: baseUrl + '/getList'
            ,limit:10
            ,limits:[10,20,50]
            ,method:'post'
            ,toolbar:'#toolbar'
            ,defaultToolbar: [{
                title: '导出'
                ,layEvent: 'exports'
                ,icon: 'layui-icon-export'
            }, 'print']
            ,title: '用户列表'
            ,cols: [[
                {field:'userAccount', title:'系统账号',type: 'text',width:180}
                ,{field:'nickName', title:'用户名',type: 'text',width:130}
                ,{field:'userName', title:'姓名',type: 'text',width:100}
                ,{field:'userSexDesc', title:'性别',type: 'text',width:80}
                ,{field:'userEmail', title:'用户邮箱',type: 'text',width:180}
                ,{field:'userTel', title:'用户电话',type: 'text',width:120}
                ,{field:'areaCode', title:'区号',type: 'text',width:150}
                ,{field:'roleName', title:'角色名称',type: 'text',width:80}
                ,{field:'createTime', title:'创建时间',type: 'text',width:160,templet:function (d) {
                        if(d.createTime){
                            return util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss")
                        }
                        return "";
                    }}
                ,{title:'操作', toolbar: '#operations',width:450}
            ]]
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(tableId)', function(obj){
            switch(obj.event){
                case 'add':
                    xadmin.open('添加用户',baseUrl + '/add');
                    break;
                case 'exports':
                    window.location.href = tool.postToGet(baseUrl + '/exports',getQueryParams());
                    break;
            };
        });
        //行工具栏事件
        table.on('tool(tableId)', function(obj){
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    xadmin.open('编辑用户',baseUrl + '/edit?id='+data.id);
                    break;
                case 'resetPassword':
                    layer.confirm('确认重置密码？', function(index){
                        var res = tool.submit(baseUrl + '/resetPassword',{id:data.id});
                        if(res.code == return_200){
                            layer.msg(res.msg, {
                                    icon: msg.success,
                                    time: 2000
                                },
                                function(){
                                    callBack();
                                }
                            );
                        }else {
                            layer.msg(res.msg, {
                                    icon: msg.error,
                                    time: 2000
                                },
                                function(){
                                    callBack();
                                }
                            );
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('确认删除此用户？', function(index){
                        var res = tool.submit(baseUrl + '/delete',{id:data.id});
                        if(res.code == return_200){
                            layer.msg(res.msg, {
                                    icon: msg.success,
                                    time: 2000
                                },
                                function(){
                                    callBack();
                                }
                            );
                        }else {
                            layer.msg(res.msg, {
                                    icon: msg.error,
                                    time: 2000
                                },
                                function(){
                                    callBack();
                                }
                            );
                        }
                    });
                    break;
            }
        });
        //条件查询
        $('#formSearch').on('click', function(){
            table.reload('tableReload', {
                page: {curr: 1 }
                ,where:  getQueryParams()
            });
        });

        /**
         * 查询的参数
         * @returns {{}}
         */
        function getQueryParams() {
            return {
                userAccount:$("#userAccount").val().trim()
                ,nickName:$("#nickName").val().trim()
                ,userName:$("#userName").val().trim()
                ,userEmail:$("#userEmail").val().trim()
                ,userTel:$("#userTel").val().trim()
                ,userSex:$("#userSex").val().trim()
                ,areaCode:$("#areaCode").val().trim()
                ,createTimeStart:$("#createTimeStart").val().trim()
                ,createTimeEnd:$("#createTimeEnd").val().trim()
            }
        }

        /**
         * 初始化页面
         */
        function init_form() {
            enums.createSelectByEnum('userSex','EnumUserSex',null,null,'请选择性别');
            form.render();
            laydate.render({
                elem: '#createTimeStart'
                ,type: 'datetime'
            });
            laydate.render({
                elem: '#createTimeEnd'
                ,type: 'datetime'
            });
        }

        /**
         * 回调函数
         */
        function callBack() {
            $("#formSearch").click();
        }

        /**
         * 导入用户数据
         */
        upload.render({
            elem: '#import'
            ,url: '/back/admin/import'
            ,accept: 'file'
            ,exts: 'xls'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    console.log(file);
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != return_200){
                    return layer.msg(res.msg,{
                        icon:msg.error,
                        time: 2000
                    });
                }
                //上传成功
                return layer.msg('文件上传成功',{
                    icon: msg.success,
                    time: 2000
                });
            }
            ,error: function(){
                return layer.msg('文件上传异常',{
                    icon: msg.error,
                    time: 2000
                });
            }
        });


    });
</script>
</body>
</html>